package cn.btks.resultintegration.achievements.service.impl;

import cn.btks.commonserver.result.Constants;
import cn.btks.commonserver.result.ResultState;
import cn.btks.commonserver.result.StateInfo;
import cn.btks.commonserver.util.FileUtil;
import cn.btks.resultintegration.achievements.pojo.LayerFeatures;
import cn.btks.resultintegration.achievements.pojo.TextAchievements;
import cn.btks.resultintegration.achievements.repository.LayerFeaturesRepository;
import cn.btks.resultintegration.achievements.service.LayerFeaturesService;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @Author lijun
 * @Date 2021/2/24 15:24
 */
@Service
@Transactional
public class LayerFeaturesServiceImpl implements LayerFeaturesService {

    @Autowired
    LayerFeaturesRepository layerFeaturesRepository;

    @Override
    public LayerFeatures findByName(String name) {
        return layerFeaturesRepository.findByName(name);
    }

    @Override
    public ResultState save(MultipartFile multipartFile, HttpServletRequest request) throws IOException {
        if(multipartFile != null){
            String fjName = UUID.randomUUID() + "_" + multipartFile.getOriginalFilename();
            String pathname = request.getSession(true).getServletContext().getRealPath("//") + Constants.ACHIEVEMENTS_TEXT_FILE + fjName;
            List<LayerFeatures> layerFeaturesList = new ArrayList<>();
            if (!FileUtil.fileUpload(multipartFile, pathname)) {
                File file = new File(pathname);
                String filePath = "";
                if(file.exists()){
                    filePath = file.getAbsolutePath();
                }
                //Excel工具项
                InputStream inputStream = new FileInputStream(filePath);
                XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
                XSSFSheet sheet = workbook.getSheetAt(0);
                for(Row row : sheet){
                    if(row.getRowNum() == 0){
                        continue;
                    }
                    LayerFeatures layerFeatures = new LayerFeatures();
                    //对应文件英文名
                    String name = row.getCell(0).getStringCellValue();
                    //管控单元分类
                    String unitClassify = row.getCell(1).getStringCellValue();
                    //环境要素
                    String environmentFeatures = row.getCell(2).getStringCellValue();
                    //要素细类
                    String detailFeatures = row.getCell(3).getStringCellValue();
                    //空间布局约束
                    String spaceRestrain = row.getCell(4).getStringCellValue();
                    //污染物排放管控
                    String pollutantRestrain = row.getCell(5).getStringCellValue();
                    //环境风险防控
                    String environmentRestrain = row.getCell(6).getStringCellValue();
                    //资源利用效率
                    String resourceUtilization = row.getCell(7).getStringCellValue();
                    String remark = row.getCell(8).getStringCellValue();

                    layerFeatures.setName(name);
                    layerFeatures.setUnitClassify(unitClassify);
                    layerFeatures.setEnvironmentFeatures(environmentFeatures);
                    layerFeatures.setDetailFeatures(detailFeatures);
                    layerFeatures.setSpaceRestrain(spaceRestrain);
                    layerFeatures.setPollutantRestrain(pollutantRestrain);
                    layerFeatures.setEnvironmentRestrain(environmentRestrain);
                    layerFeatures.setResourceUtilization(resourceUtilization);
                    layerFeatures.setRemarks(remark);
                    layerFeaturesList.add(layerFeatures);
                }
            }
            if(layerFeaturesList.size() > 0){
                List<LayerFeatures> layerFeaturesList1 = layerFeaturesRepository.saveAll(layerFeaturesList);
                return layerFeaturesList1 != null && layerFeaturesList1.size() > 0 ? new ResultState(StateInfo.Upload_File_OK.getCode(), StateInfo.Upload_File_OK.getMsg(), layerFeaturesList1) :
                        new ResultState(StateInfo.Upload_File_ERROR.getCode(), StateInfo.Upload_File_ERROR.getMsg());
            }
        }
        return new ResultState(StateInfo.Upload_File_ERROR.getCode(), StateInfo.Upload_File_ERROR.getMsg());
    }

    @Override
    public ResultState update(MultipartFile multipartFile, HttpServletRequest request) throws IOException {
        if(multipartFile != null){
            String fjName = UUID.randomUUID() + "_" + multipartFile.getOriginalFilename();
            String pathname = request.getSession(true).getServletContext().getRealPath("//") + Constants.ACHIEVEMENTS_TEXT_FILE + fjName;
            List<LayerFeatures> layerFeaturesList = new ArrayList<>();
            if (!FileUtil.fileUpload(multipartFile, pathname)) {
                File file = new File(pathname);
                String filePath = "";
                if(file.exists()){
                    filePath = file.getAbsolutePath();
                }
                //Excel工具项
                InputStream inputStream = new FileInputStream(filePath);
                XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
                XSSFSheet sheet = workbook.getSheetAt(0);
                for(Row row : sheet){
                    if(row.getRowNum() == 0){
                        continue;
                    }
                    LayerFeatures layerFeatures = new LayerFeatures();
                    //对应文件英文名
                    String name = row.getCell(0).getStringCellValue();
                    //管控单元分类
                    String unitClassify = row.getCell(1).getStringCellValue();
                    //环境要素
                    String environmentFeatures = row.getCell(2).getStringCellValue();
                    //要素细类
                    String detailFeatures = row.getCell(3).getStringCellValue();
                    //空间布局约束
                    String spaceRestrain = row.getCell(4).getStringCellValue();
                    //污染物排放管控
                    String pollutantRestrain = row.getCell(5).getStringCellValue();
                    //环境风险防控
                    String environmentRestrain = row.getCell(6).getStringCellValue();
                    //资源利用效率
                    String resourceUtilization = row.getCell(7).getStringCellValue();
                    String remark = row.getCell(8).getStringCellValue();

                    layerFeatures.setName(name);
                    layerFeatures.setUnitClassify(unitClassify);
                    layerFeatures.setEnvironmentFeatures(environmentFeatures);
                    layerFeatures.setDetailFeatures(detailFeatures);
                    layerFeatures.setSpaceRestrain(spaceRestrain);
                    layerFeatures.setPollutantRestrain(pollutantRestrain);
                    layerFeatures.setEnvironmentRestrain(environmentRestrain);
                    layerFeatures.setResourceUtilization(resourceUtilization);
                    layerFeatures.setRemarks(remark);
                    layerFeaturesList.add(layerFeatures);
                }
            }
            if(layerFeaturesList.size() > 0){
                layerFeaturesRepository.deleteAllFeatures();
                List<LayerFeatures> layerFeaturesList1 = layerFeaturesRepository.saveAll(layerFeaturesList);
                return layerFeaturesList1 != null && layerFeaturesList1.size() > 0 ? new ResultState(StateInfo.Upload_File_OK.getCode(), StateInfo.Upload_File_OK.getMsg(), layerFeaturesList1) :
                        new ResultState(StateInfo.Upload_File_ERROR.getCode(), StateInfo.Upload_File_ERROR.getMsg());
            }
        }
        return new ResultState(StateInfo.Upload_File_ERROR.getCode(), StateInfo.Upload_File_ERROR.getMsg());
    }

    @Override
    public int deleteAllFeatures() {
        return layerFeaturesRepository.deleteAllFeatures();
    }
}
